package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.model.Goods;
import com.atguigu.gmall.list.model.SearchParam;
import com.atguigu.gmall.list.model.SearchResponseVo;
import com.atguigu.gmall.list.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author: atguigu
 * @create: 2023-03-03 10:10
 */
@Slf4j
@RestController
@RequestMapping("api/list")
public class ListApiController {


    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    private SearchService searchService;


    /**
     * 提前将索引库创建,同时设置映射信息
     *
     * @return
     */
    @GetMapping("inner/createIndex")
    public Result createIndex() {
        elasticsearchRestTemplate.createIndex(Goods.class);
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }

    /**
     * 该接口仅仅用于测试:将指定商品SkuID封装为索引库商品文档对象,将商品存入索引库
     *
     * @param skuId
     * @return
     */
    @GetMapping("/inner/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable("skuId") Long skuId) {
        searchService.upperGoods(skuId);
        return Result.ok();
    }


    /**
     * 该接口仅仅用于测试:将指定商品从索引库删除
     *
     * @param skuId
     * @return
     */
    @GetMapping("/inner/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable("skuId") Long skuId) {
        searchService.lowerGoods(skuId);
        return Result.ok();
    }


    /**
     * 提供给详情服务调用:更新商品热度分值
     *
     * @param skuId
     */
    @GetMapping("/inner/incrHotScore/{skuId}")
    public void incrHotScore(@PathVariable("skuId") Long skuId) {
        searchService.incrHotScore(skuId);
    }


    /**
     * 提供给前端服务/移动端,商品检索
     *
     * @param searchParam
     * @return
     */
    @PostMapping({"", "/"})
    public Result search(@RequestBody SearchParam searchParam) {
        SearchResponseVo responseVo = searchService.search(searchParam);
        return Result.ok(responseVo);
    }


    @GetMapping("/testLog")
    public void testLog(){
        log.debug("debug级别日志");
        log.info("info级别日志");
        log.warn("warn级别日志");
        log.error("error级别日志");
    }
}
